<template>
  <div class="editable-cell" @click="edit">
    <div v-show="editable" class="editable-cell-input-wrapper">
      <a-input ref="editInput" :value="value" @change="handleChange" @blur="check" @pressEnter="check" />
      <!-- <a-icon
        type="check"
        class="editable-cell-icon-check"
        @click="check"
      /> -->
    </div>
    <div v-show="!editable" class="editable-cell-text-wrapper">
      {{ value || ' ' }}
      <a-icon type="edit" class="editable-cell-icon" @click="edit" />
    </div>
  </div>
</template>
<script>
export default {
  props: {
    text: {
      type: String,
      required: false,
      default: ''
    }
  },
  data () {
    return {
      value: this.text,
      editable: false
    }
  },
  methods: {
    handleChange (e) {
      const value = e.target.value
      this.value = value
    },
    check () {
      this.editable = false
      this.$emit('change', this.value)
    },
    edit () {
      this.editable = true
      this.$nextTick(() => {
        this.$refs.editInput.focus()
      })
    }
  }
}
</script>
